package com.ftcs.model;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * Web Result
 *
 * @author changhuafeng
 * @version V1.0.0
 * @since 2019/3/4
 */
@Data
@AllArgsConstructor
public class BaseResult<T> implements Serializable {

    @JSONField(ordinal = -1030)
    @Builder.Default
    private boolean success = true;

    @JSONField(ordinal = -1020)
    private int code; // 接口返回消息编码


    @JSONField(ordinal = -1010)
    private String message;// 提示信息

    @JSONField(ordinal = -990)
    private T data; // 默认返回结果

    public BaseResult() {
        this(true, 10000, "");
    }

    public BaseResult(boolean success) {
        this(success, 10000, "", null);
    }

    public BaseResult(boolean success, Integer code, String message) {
        this(success, code, message, null);
    }

    public BaseResult(boolean success, Integer code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 直接返回true
     *
     * @return BaseResult
     */
    public static BaseResult success() {
        return new BaseResult(true, null, null);
    }

    public static <T> BaseResult success(T data) {
        return new BaseResult(true, 10000, "", data);
    }

    public static <T> BaseResult success(T data,String message) {
        return new BaseResult(true, 10000, message, data);
    }

    public static BaseResult fail() {
        return fail(null);
    }

    public static BaseResult fail(Integer code) {
        return fail(code, null);
    }

    public static BaseResult fail(Integer code, String message) {
        BaseResult result = new BaseResult();

        result.setSuccess(false);
        result.setCode(code);
        result.setMessage(message);
        return result;

    }
}
